/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package fase2.pt.iul.sid.database;

import fase2.pt.iul.sid.model.PratoModel;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author Ricardo Marques
 */
public class PratoDAO {
    
    private FactoryConnection fc = null;
    
    public PratoDAO(FactoryConnection fc){
        this.fc = fc;
    }
    
    public PratoModel getPrato(int id) {
       
        PratoModel prato = null;

        int resultsetType =0;
        ConnectionHandler ch = new ConnectionHandler();
                
        try {
            fc.returnResultSetSelectStatement("SELECT * FROM Prato WHERE idPrato=" + id + " ", resultsetType, ch);

            if(ch.r!=null){
                if(ch.r.next()){
                    
                    prato = new PratoModel(ch.r.getInt("idPrato"),ch.r.getDouble("preco"),ch.r.getString("descricao"),ch.r.getDouble("rating"));
                    
                }
            }
            
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        return prato;
        
    }
    
    public List<PratoModel> getPratos(int idEstabelecimento) {
       
        List<PratoModel> pratos = new LinkedList<PratoModel>();

        int resultsetType =0;
        ConnectionHandler ch = new ConnectionHandler();
                
        try {
            
            if(idEstabelecimento==0){
                fc.returnResultSetSelectStatement("SELECT m.idPrato, p.preco,p.descricao,p.rating FROM Prato p INNER JOIN menuDoEstabelecimento m on m.idPrato=p.idPrato INNER JOIN Estabelecimento e on m.idEstabelecimento=e.idEstabelecimento", resultsetType, ch);
            }else{
                fc.returnResultSetSelectStatement("SELECT m.idPrato, p.preco,p.descricao,p.rating FROM Prato p INNER JOIN menuDoEstabelecimento m on m.idPrato=p.idPrato INNER JOIN Estabelecimento e on m.idEstabelecimento=e.idEstabelecimento where m.idEstabelecimento="+idEstabelecimento, resultsetType, ch);
            }
            
            if(ch.r!=null){
                                
                pratos.add(new PratoModel(0, 0.0, "Todos", 0.0));
                
                while(ch.r.next()){
                    
                    PratoModel prato = new PratoModel(ch.r.getInt("idPrato"),ch.r.getDouble("preco"),ch.r.getString("descricao"),ch.r.getDouble("rating"));
                    pratos.add(prato);
                    
                }
            }
            
        } catch (SQLException e) {
            e.printStackTrace();
        }
        
        return pratos;
      
   }
    
}
